
import os

def execute_epw2wea(bin_dir, source_epwfile, destination_weafile):
    command = "epw2wea " + source_epwfile  + " " + destination_weafile 
    os.chdir(bin_dir)
    os.system(command)

def write_weather_data(wea_file_dir, ground_reflectance,short_wea_file_name):
    results = open(wea_file_dir, "r")
    result_stuff = results.readlines()
    data_list = []
    for line in range(0,5):
        data = result_stuff[line]
        data_list.append(data)
    weather_data = "#site_information2\n\n"+\
    data_list[0]+\
    data_list[1]+\
    data_list[2]+\
    data_list[3]+\
    data_list[4]+\
    "ground_reflectance " + str(ground_reflectance) + "\n"+\
    "wea_data_file " + wea_file_dir + "\n"+\
    "wea_data_short_file " + "wea/" + short_wea_file_name + "\n\n"+\
    "#############################################################\n\n"
    return weather_data

def write_proj_info(proj_name, proj_dir, tmp_dir):
    project_data = "#project_info\n\n"+\
    "project_name            " + proj_name + "\n"+\
    "project_directory       " + proj_dir + "\n"+\
    "tmp_directory           " + tmp_dir + "\n\n"+\
    "#############################################################\n\n"
    return project_data

def write_geometry_info(proj_name, sensor_file_dir, rad_file_dir_list):
    geometry_data = "#building_information\n\n"+\
    "material_file " + "rad/" + proj_name +"_material.rad\n"+\
    "geometry_file " + "rad/" + proj_name +"_geometry.rad\n"+\
    "scene_rotation_angle 00.00\n"+\
    "sensor_file " + sensor_file_dir + "\n"+\
    "radiance_source_files " + str(len(rad_file_dir_list)) + ","
    #print len(rad_file_dir_list)
    for counter in range(len(rad_file_dir_list)):
        rad_file_dir = rad_file_dir_list[counter]
	print rad_file_dir
        if counter < len(rad_file_dir_list)-1:
            geometry_data = geometry_data + rad_file_dir + ", "
        else:
            geometry_data = geometry_data + rad_file_dir + "\n"
    
    geometry_data =geometry_data + "shading 1\n"+\
    "static_system " + "res/" + proj_name + ".dc " + "res/" + proj_name + ".ill\n"+\
    "sensor_file_unit " + "0 0 0 0 0 0 0 0 0\n"+\
    "ViewPoint " + "9\n\n"+\
    "#############################################################\n\n"
    
    return geometry_data

def write_result_file(proj_name):
    result_file_data = "#daylight_analysis_results\n\n"+\
    "daylight_autonomy " + "res/" + proj_name + ".da\n"+\
    "daylight_factor " + "res/" + proj_name + ".df\n"+\
    "electric_lighting " + "res/" + proj_name + ".el.htm\n"+\
    "direct_sunlight_file " + "res/" + proj_name + ".dir\n"+\
    "thermal_simulation " + "res/" + proj_name + ".intgain" +".csv\n"+\
    "daylight_factor_RGB " + "res/" + proj_name + ".daylight_factor.DA\n"+\
    "daylight_autonomy_active_RGB " + "res/" + proj_name + ".daylight_autonomy.DA\n"+\
    "continuous_daylight_autonomy_active_RGB " + "res/" + proj_name + ".continuous_daylight_autonomy.DA\n"+\
    "DA_max_active_RGB " + "res/" + proj_name + ".DA_max.DA\n"+\
    "UDI_100_active_RGB " + "res/" + proj_name + ".UDI_100.DA\n"+\
    "UDI_100_2000_active_RGB " + "res/" + proj_name + ".UDI_100_2000.DA\n"+\
    "UDI_2000_active_RGB " + "res/" + proj_name + ".UDI_2000.DA\n"+\
    "DSP_active_RGB " + "res/" + proj_name + ".DaylightSaturationPercentage.DA\n\n"+\
    "#############################################################\n\n"
    return result_file_data

def write_zone_area(zone_area):
    zone_info = "    zone_description 'zone'\n" +\
    "    zone_area " + zone_area + "\n\n"
    return zone_info

def write_user_description(occupancy_start, occupancy_end, min_illum_level):
    user_description_info = "    ===================\n"+\
    "    = user description\n" +\
    "    ===================\n" +\
    "    occupancy 0 " + occupancy_start + " " + occupancy_end + "\n" +\
    "    minimum_illuminance_level " + min_illum_level + "\n\n"
    return user_description_info
    
